import cn.gzzhome.utils.CommandBuilder
import cn.gzzhome.utils.CommandParamManager

/**
 * curl命令
 * @param cb 参数设置
 * @return 成功时返回curl的输出
 */
def call(Closure cb) {

    CommandBuilder builder = new CommandBuilder()
    if (Utils({ commandExists("curl") })) {
        CommandParamManager manager = new CommandParamManager([
                ["method", "请求方法：post/get/put/option/head", {
                    values ? "-X ${values[0].toString().toUpperCase()}" : ""
                }],
                ["redirectAuto", "自动重定向，默认为true", builder.ifTrue("-L", true)],
                ["upload", "要上传的文件路径，格式：'<参数名>=@<文件路径>'", builder.allValue("-F")],
                ["url", "要访问的url全路径", builder.oneValue("", ""), true]
        ])
        def cmd = manager.build("curl", cb)
        return sh(script: "${cmd}", returnStdout: true)
    } else {
        error("curl命令不存在！")
    }
}